package com.gisocn.onemap.presenter;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import com.gisocn.onemap.base.BasePresenter;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.model.AppConfig;
import com.gisocn.onemap.model.AppVersion;
import com.gisocn.onemap.model.CheckDeviceModel;
import com.gisocn.onemap.model.GetPostRequest;
import com.gisocn.onemap.model.GetUserConfig;
import com.gisocn.onemap.model.LoginModel;
import com.gisocn.onemap.model.RegisterModel;
import com.gisocn.onemap.model.Update;
import com.gisocn.onemap.net.ApiCallback;
import com.gisocn.onemap.utils.AppVersionManager;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.utils.PullHelper;
import com.gisocn.onemap.utils.Utils;
import com.gisocn.onemap.view.LoginView;
import com.google.gson.Gson;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

/* loaded from: classes2.dex */
public class LoginPresenter extends BasePresenter<LoginView> {
    private AppConfig appConfig = new AppConfig();
    private Context context;

    public LoginPresenter(LoginView loginView, Context context) {
        this.context = context;
        attachView(loginView);
    }

    public void getVersion(String str) {
        new QueryAddressTask(str).execute(new String[0]);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes2.dex */
    public class QueryAddressTask extends AsyncTask<String, String, String> {
        String url;

        public QueryAddressTask(String str) {
            this.url = str;
            Log.e("QueryAddressTask", str);
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        public String doInBackground(String... strArr) {
            String str;
            Exception e;
            try {
                Log.e("QueryAddressTask", System.currentTimeMillis() + "");
                str = LoginPresenter.this.getVersionInfo(this.url);
                try {
                    Log.e("getResponse", System.currentTimeMillis() + "" + str);
                } catch (Exception e2) {
                    e = e2;
                    e.printStackTrace();
                    return str;
                }
            } catch (Exception e3) {
                str = "";
                e = e3;
            }
            return str;
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        public void onPostExecute(String str) {
            if (str != null && !str.equals("")) {
                Log.e("result________________", str + "+++++");
                Update update = (Update) new Gson().fromJson(str, (Class<Object>) Update.class);
                if (update.getVersionNumber() == null) {
                    return;
                }
                Log.e("getVersionNumber:", update.getVersionNumber());
                Log.e("getVersionUrl:", update.getVersionUrl());
                if (!update.getVersionNumber().equals(Utils.getVersionName(LoginPresenter.this.context))) {
                    AppVersionManager appVersionManager = new AppVersionManager((Activity) LoginPresenter.this.context);
                    appVersionManager.upDateAppVersion(update.getVersionUrl(), update.getVersionNumber());
                    appVersionManager.updaResult(new AppVersionManager.IUpdataResult() { // from class: com.gisocn.onemap.presenter.LoginPresenter.QueryAddressTask.1
                        @Override // com.gisocn.onemap.utils.AppVersionManager.IUpdataResult
                        public void updataResult() {
                            Log.d("取消更新:", "取消更新:");
                            if (LoginPresenter.this.mvpView != 0) {
                                ((LoginView) LoginPresenter.this.mvpView).updaeFinish();
                            }
                        }
                    });
                } else if (LoginPresenter.this.mvpView != 0) {
                    ((LoginView) LoginPresenter.this.mvpView).updaeFinish();
                }
            } else if (LoginPresenter.this.mvpView != 0) {
                ((LoginView) LoginPresenter.this.mvpView).updaeFinish();
            }
        }
    }

    public String getVersionInfo(String str) throws Exception {
        SoapObject soapObject = new SoapObject("http://tempuri.org/", "MultipleVersion");
        soapObject.addProperty("Project", Constant.PROJECTSGZX);
        SoapSerializationEnvelope soapSerializationEnvelope = new SoapSerializationEnvelope(120);
        soapSerializationEnvelope.bodyOut = soapObject;
        soapSerializationEnvelope.dotNet = true;
        new HttpTransportSE(str, 5000).call(null, soapSerializationEnvelope);
        SoapObject soapObject2 = (SoapObject) soapSerializationEnvelope.bodyIn;
        L.e("" + soapObject2.toString());
        return soapObject2.getProperty(0).toString();
    }

    public void doLogin(Map<String, String> map) {
        addSubscription(this.apiService.doUserLogin(map), new ApiCallback<LoginModel>() { // from class: com.gisocn.onemap.presenter.LoginPresenter.1
            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFinish() {
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onSuccess(LoginModel loginModel) {
                if (loginModel.getReturnCode() == 0) {
                    ((LoginView) LoginPresenter.this.mvpView).userLoginSuccess(loginModel);
                } else {
                    ((LoginView) LoginPresenter.this.mvpView).userLoginFail(loginModel.getMessage());
                }
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFailure(String str) {
                ((LoginView) LoginPresenter.this.mvpView).userLoginFail(str);
            }
        });
    }

    public void checkDevice(Map<String, String> map) {
        addSubscription(this.apiService.checkDevice(map), new ApiCallback<CheckDeviceModel>() { // from class: com.gisocn.onemap.presenter.LoginPresenter.2
            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFinish() {
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onSuccess(CheckDeviceModel checkDeviceModel) {
                if (checkDeviceModel.getReturnCode() == 0) {
                    ((LoginView) LoginPresenter.this.mvpView).checkDeviceSuccess(checkDeviceModel);
                } else {
                    ((LoginView) LoginPresenter.this.mvpView).checkDeviceFail(checkDeviceModel.getMessage());
                }
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFailure(String str) {
                ((LoginView) LoginPresenter.this.mvpView).showFailMsg(str);
            }
        });
    }

    public void registerDevice(GetPostRequest getPostRequest) {
        Log.e("GetPostRequest", new Gson().toJson(getPostRequest));
        ((LoginView) this.mvpView).showLoading();
        addSubscription(this.apiService.registerDevice(getPostRequest), new ApiCallback<RegisterModel>() { // from class: com.gisocn.onemap.presenter.LoginPresenter.3
            @Override // com.gisocn.onemap.net.ApiCallback
            public void onSuccess(RegisterModel registerModel) {
                if (registerModel.getRegisterDeviceResult().getReturnCode() == 0) {
                    ((LoginView) LoginPresenter.this.mvpView).registerDeviceSuccess(registerModel);
                } else {
                    ((LoginView) LoginPresenter.this.mvpView).registerDeviceFail(registerModel.getRegisterDeviceResult().getMessage());
                }
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFailure(String str) {
                ((LoginView) LoginPresenter.this.mvpView).registerDeviceFail(str);
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFinish() {
                ((LoginView) LoginPresenter.this.mvpView).hideLoading();
            }
        });
    }

    public void getAppConfigData(boolean z) {
        L.e("getAppConfigData______" + Constant.SAVE_PATH);
        try {
            File file = new File(Constant.SAVE_PATH + Constant.APPCONFIG_XML_NAME);
            AppConfig appConfig = (AppConfig) new Gson().fromJson(PullHelper.getAppConfig(this.context.getAssets().open(Constant.APPCONFIG_XML_NAME)), (Class<Object>) AppConfig.class);
            this.appConfig = appConfig;
            long j = 0;
            long parseLong = appConfig.getVersion() != null ? Long.parseLong(this.appConfig.getVersion()) : 0L;
            L.e(" version:" + parseLong);
            if (!file.exists()) {
                writeToLocalXml(file);
                ((LoginView) this.mvpView).getAppConfig(this.appConfig);
                return;
            }
            AppConfig appConfig2 = (AppConfig) new Gson().fromJson(PullHelper.getAppConfig(new FileInputStream(file)), (Class<Object>) AppConfig.class);
            if (appConfig2.getVersion() != null && !TextUtils.isEmpty(appConfig2.getVersion())) {
                j = Long.parseLong(appConfig2.getVersion());
                L.e("version1:" + j);
                if (parseLong >= j) {
                    writeToLocalXml(file);
                    ((LoginView) this.mvpView).getAppConfig(this.appConfig);
                } else {
                    ((LoginView) this.mvpView).getAppConfig(appConfig2);
                }
            } else {
                writeToLocalXml(file);
                L.e("version1:0");
                ((LoginView) this.mvpView).getAppConfig(this.appConfig);
            }
            StringBuilder sb = new StringBuilder();
            sb.append("result");
            sb.append(parseLong >= j ? "assets" : "local");
            L.e(sb.toString());
        } catch (IOException e) {
            ((LoginView) this.mvpView).getAppConfigFail("xml配置加载失败");
            e.printStackTrace();
        } catch (XmlPullParserException e2) {
            ((LoginView) this.mvpView).getAppConfigFail("xml配置加载失败");
            e2.printStackTrace();
        }
    }

    private void writeToLocalXml(File file) {
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            file.delete();
            try {
                file.createNewFile();
            } catch (IOException e2) {
                e2.printStackTrace();
            }
        }
        byte[] bArr = new byte[1024];
        try {
            InputStream open = this.context.getAssets().open(Constant.APPCONFIG_XML_NAME);
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            while (true) {
                int read = open.read(bArr);
                if (read != -1) {
                    fileOutputStream.write(bArr, 0, read);
                } else {
                    fileOutputStream.flush();
                    open.close();
                    fileOutputStream.close();
                    return;
                }
            }
        } catch (FileNotFoundException e3) {
            e3.printStackTrace();
        } catch (IOException e4) {
            e4.printStackTrace();
        }
    }

    public void getAppConfigData(String str, String str2) {
        addSubscription(this.apiService.getUserConfig(str, str2), new ApiCallback<GetUserConfig>() { // from class: com.gisocn.onemap.presenter.LoginPresenter.4
            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFinish() {
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onSuccess(GetUserConfig getUserConfig) {
                if (getUserConfig.getReturnCode() == 0) {
                    File file = new File(Constant.SAVE_PATH + Constant.APPCONFIG_XML_NAME);
                    if (!file.exists()) {
                        try {
                            file.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } else {
                        file.delete();
                        try {
                            file.createNewFile();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                    }
                    byte[] bytes = getUserConfig.getMessage().getBytes();
                    try {
                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        try {
                            fileOutputStream.write(bytes);
                            fileOutputStream.close();
                        } catch (IOException e3) {
                            e3.printStackTrace();
                        }
                    } catch (FileNotFoundException e4) {
                        e4.printStackTrace();
                    }
                    ((LoginView) LoginPresenter.this.mvpView).getXMLAppConfigSuccess();
                    return;
                }
                ((LoginView) LoginPresenter.this.mvpView).getXMLAppConfigFail("获取在线配置文件失败," + getUserConfig.getMessage());
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFailure(String str3) {
                ((LoginView) LoginPresenter.this.mvpView).getXMLAppConfigFail(str3);
            }
        });
    }

    public void getAppVersion(String str, Map<String, String> map) {
        addSubscription(this.apiService.getAppVersion(str, map), new ApiCallback<AppVersion>() { // from class: com.gisocn.onemap.presenter.LoginPresenter.5
            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFinish() {
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onSuccess(AppVersion appVersion) {
                ((LoginView) LoginPresenter.this.mvpView).getAppVersionSuccess(appVersion);
            }

            @Override // com.gisocn.onemap.net.ApiCallback
            public void onFailure(String str2) {
                ((LoginView) LoginPresenter.this.mvpView).getAppVersionFail(str2);
            }
        });
    }

    public void getAppConfigData() {
        InputStream fileInputStream;
        L.e("getAppConfigData______" + Constant.SAVE_PATH);
        try {
            File file = new File(Constant.SAVE_PATH + Constant.APPCONFIG_XML_NAME);
            if (!file.exists()) {
                fileInputStream = this.context.getAssets().open(Constant.APPCONFIG_XML_NAME);
            } else {
                L.e("xmlFile.exists()" + file.getPath());
                fileInputStream = new FileInputStream(file);
            }
            this.appConfig = (AppConfig) new Gson().fromJson(PullHelper.getAppConfig(fileInputStream), (Class<Object>) AppConfig.class);
            ((LoginView) this.mvpView).getAppConfig(this.appConfig);
        } catch (IOException e) {
            ((LoginView) this.mvpView).getAppConfigFail("xml配置加载失败");
            e.printStackTrace();
        } catch (XmlPullParserException e2) {
            ((LoginView) this.mvpView).getAppConfigFail("xml配置加载失败");
            e2.printStackTrace();
        }
    }
}
